from django.shortcuts import render

# Create your views here.
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login as auth_login
from django.shortcuts import render,redirect

from .forms import SignUpForm

def signup(request):
    # return render(request,'signup.html')
    """
    form = UserCreationForm()
    return render(request,'signup.html',{'form': form})
    """

    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            # 表单有效,就创建一个User实例
            user = form.save()
            # 将创建的用户作为参数传递给auth_login函数,手动验证用户.
            auth_login(request,user)
            # 视图将用户重定向到主页,保持应用程序.
            return redirect('home')

    else:
        form = SignUpForm()
    return render(request,'signup.html',{'form': form})



